package pl.gwt.client;

import pl.gwt.client.gui.RightPanelMain;
import pl.gwt.client.gui.TopPanel;
import pl.gwt.client.gui.Shortcuts;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.WindowResizeListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class CRMEntryPoint implements EntryPoint, WindowResizeListener {
	private static CRMEntryPoint singleton;
	DockPanel outer = new DockPanel();

	private static final Images images = GWT.create(Images.class);

	public interface Images extends TopPanel.Images, Shortcuts.Images {
	}

	public static CRMEntryPoint get() {
		return singleton;
	}

	private TopPanel topPanel = new TopPanel(images);
	private Shortcuts shortcuts = new Shortcuts(images);
	private Widget rightPanel = new RightPanelMain();

	public void onModuleLoad() {
		singleton = this;

		topPanel.setWidth("100%");

		outer.add(topPanel, DockPanel.NORTH);
		outer.add(shortcuts, DockPanel.WEST);
		outer.add(rightPanel, DockPanel.CENTER);

		outer.setWidth("100%");
		outer.setSpacing(4);
		
		Window.addWindowResizeListener(this);

		Window.enableScrolling(false);
		Window.setMargin("0px");

		RootPanel.get().add(outer);

		DeferredCommand.addCommand(new Command() {
			public void execute() {
				onWindowResized(Window.getClientWidth(), Window
						.getClientHeight());
			}
		});
	}

	public void onWindowResized(int width, int height) {
		// Adjust the shortcut panel and detail area to take up the available
		// room
		// in the window.
		int shortcutHeight = height - shortcuts.getAbsoluteTop() - 8;
		if (shortcutHeight < 1) {
			shortcutHeight = 1;
		}
		shortcuts.setHeight(shortcutHeight + "px");
		
		int widthPx = width - shortcuts.getOffsetWidth() - 10;
		
//		rightPanel.setWidth(Integer.toString(widthPx) + "px");
//		rightPanel.setHeight((shortcutHeight - 30) + "px");
		rightPanel.setWidth("100%");
		rightPanel.setHeight("100%");

	}

	public void changeRightPanel(Widget rp) {
		this.rightPanel.removeFromParent();
		this.rightPanel = rp;
		outer.add(rightPanel, DockPanel.CENTER);
		onWindowResized(Window.getClientWidth(), Window.getClientHeight());
	}

}
